/** 
 * Copyright 2014 whj All Rights Reserved.
 * 项目名：codegnrt
 * 包名：com.whj.code.dao.mybatis
 * 文件名：MybatisMapperGenerator.java 
 * 日期：2014-11-25 10:21
 */
package com.whj.code.dao.mybatis;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.codegen.AbstractXmlGenerator;
import org.mybatis.generator.codegen.mybatis3.javamapper.JavaMapperGenerator;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
import org.mybatis.generator.config.PropertyRegistry;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 生成mapper类.
 * @author wanghj
 *
 */
public class MybatisMapperGenerator extends JavaMapperGenerator{

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
                introspectedTable.getFullyQualifiedTable().toString()));
        CommentGenerator commentGenerator = context.getCommentGenerator();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getMyBatis3JavaMapperType());
        Interface interfaze = new Interface(type);
        interfaze.setVisibility(JavaVisibility.PUBLIC);
        commentGenerator.addJavaFileComment(interfaze);

        String rootInterface = introspectedTable
            .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
        if (!stringHasValue(rootInterface)) {
            rootInterface = context.getJavaClientGeneratorConfiguration()
                .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
        }

        if (stringHasValue(rootInterface)) {
            FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
                    rootInterface);
            interfaze.addSuperInterface(fqjt);
            interfaze.addImportedType(fqjt);
        }
        
        addCountByExampleMethod(interfaze);
        addDeleteByExampleMethod(interfaze);
        addDeleteByPrimaryKeyMethod(interfaze);
        addInsertMethod(interfaze);
        addInsertSelectiveMethod(interfaze);
        addSelectByExampleWithBLOBsMethod(interfaze);
        addSelectByExampleWithoutBLOBsMethod(interfaze);
        addSelectByPrimaryKeyMethod(interfaze);
        addUpdateByExampleSelectiveMethod(interfaze);
        addUpdateByExampleWithBLOBsMethod(interfaze);
        addUpdateByExampleWithoutBLOBsMethod(interfaze);
        addUpdateByPrimaryKeySelectiveMethod(interfaze);
        addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
        addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);

        List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
        if (context.getPlugins().clientGenerated(interfaze, null,
                introspectedTable)) {
            answer.add(interfaze);
        }
        
        List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
        if (extraCompilationUnits != null) {
            answer.addAll(extraCompilationUnits);
        }
        
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        
        StringBuffer fileCommentSb = new StringBuffer();
        fileCommentSb.append("/**\n");
        fileCommentSb.append(" * Copyright " + year + " whj All Rights Reserved. \n");
        fileCommentSb.append(" * 项目名：\n");
        fileCommentSb.append(" * 包名：").
            append(context.getSqlMapGeneratorConfiguration().getTargetPackage()).append("\n");
        fileCommentSb.append(" * 文件名：").
            append(context.getTableConfigurations().get(0).getDomainObjectName()).append("Mapper").append(".java\n");
        fileCommentSb.append(" * 日期：").append(new Date()).append("\n");
        fileCommentSb.append(" */");
        
        interfaze.addFileCommentLine(fileCommentSb.toString());
        

        StringBuilder sb = new StringBuilder();
        interfaze.addJavaDocLine("/**"); //$NON-NLS-1$
        sb.append(" * 表： "); //$NON-NLS-1$
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append(" 的持久层接口").append(".");
        interfaze.addJavaDocLine(sb.toString());
        interfaze.addJavaDocLine(" * @author wanghaijun");
        interfaze.addJavaDocLine(" */"); //$NON-NLS-1$
        
        return answer;
    }

    protected void addInsertMethod(Interface interfaze) {
        if (introspectedTable.getRules().generateInsert()) {
            AbstractJavaMapperMethodGenerator methodGenerator = new InsertMethodGenerator();
            initializeAndExecuteGenerator(methodGenerator, interfaze);
        }
    }

    protected void addUpdateByPrimaryKeySelectiveMethod(Interface interfaze) {

    }

    protected void addInsertSelectiveMethod(Interface interfaze) {
        
    }
    
    @Override
    public AbstractXmlGenerator getMatchedXMLGenerator() {
        return new MybatisXMLMapperGenerator();
    }
}
